<?php
	include "Model/DBUtil.php";
	
	class Article
	{
		private $id;
		private $name;
		private $description;
		private $price;
		private $image;
		private $memberId;
		
		public function __construct() { }
		
		public static function WithRow($row){
			$instance = new self();
			$instance->LoadRow($row);
			return $instance;
		}
		
		public static function WithParams($name, $description, $image){
			$instance = new self();
			$instance->SetName($name);
			$instance->SetDescription($description);
			$instance->SetImage($image);
			return $instance;
		}
		
		public function GetName(){
			return $this->name;
		}
		
		public function SetName($name){
			$this->name = $name;
		}
		
		public function GetDescription(){
			return $this->description;
		}
		
		public function SetDescription($description){
			$this->description = $description;
		}
		
		public function GetImage(){
			return $this->image;
		}
		
		public function SetImage($image){
			$this->image = $image;
		}
		
		public function GetId(){
			return $this->id;
		}
		
		/**
 		* returns an array of Article objects for the specified page number
 		* @param unknown_type $conn
 		* @param integer $page
 		* @param integer &$noOfPages
 		*/
		public static function GetFeaturedArticles($conn, $page, &$noOfPages){
			$ba = array(new t(':pages', 0));
			$results = DBUtil::ExecuteProcedureQuery($conn, "BEGIN ARTICLES_PKG.article_get2($page,6,:pages,:rc); END;", $ba);
			$noOfPages = $ba[0];
			
			if (is_string($results)){
				return $results;
			}
			
			$objectList = array();
			foreach ($results as $result){
				array_push($objectList, Article::WithRow($result));
			}
			
			return $objectList;
		}
		
		private function LoadRow($row){
			$this->id = $row['ID'];
			$this->name = $row['NAME'];
			$this->description = $row['DESCRIPTION'];
		}
		
		public function Save($conn){
			$stmt = "BEGIN ARTICLE_PKG.article_save('%s','%s','%s'); END;";
			$stmt = sprintf($stmt, $this->name, $this->description, $this->image);

			return DBUtil::ExecuteProcedureNonQuery($conn, $stmt);
		}
		
		public function __toString(){
        	return (string)$this->id;
    	}
		
	}
?>
